package com.xhd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xhd.Dao.SalerDao;
import com.xhd.Dao.SalerDaoImpl;
import com.xhd.vo.Fruits;
import com.xhd.vo.Order;
import com.xhd.vo.Yixiang;
import com.xhd.vo.Tuijian;

/**
 * Servlet implementation class ChoujiangServlet
 */
@WebServlet("/ChoujiangServlet")
public class ChoujiangServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChoujiangServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

			doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//System.out.println("aaa");
		//解决中文乱码
		response.setContentType("text/html;charset=utf-8");
		//请求解决乱码
		request.setCharacterEncoding("utf-8");
		//响应解决乱码
		response.setCharacterEncoding("utf-8");
		
		String method = request.getParameter("method");
		if(method.equals("addYixiang")) {
			try {
				addYixiang(request,response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(method.equals("findby")) {
			try {
				findby(request,response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(method.equals("addTuijian")) {
			try {
				addTuijian(request,response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(method.equals("delTuijian")) {
			try {
				delTuijian(request,response);
			} catch (ServletException | IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	protected void addYixiang(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/utf-8;charset=utf-8");
    
        PrintWriter out = response.getWriter();
       String name=request.getParameter("name");
       String phonenum=request.getParameter("phonenum");
       String fruit=request.getParameter("fruit");
       String address=request.getParameter("address");
       String sheng=request.getParameter("sheng");
       String shi=request.getParameter("shi");
       String qu=request.getParameter("qu");
       String jiaoyifangshi=request.getParameter("jiaoyifangshi");
       Yixiang yixiang=new Yixiang(name,phonenum,fruit,address,jiaoyifangshi,sheng,shi,qu);
	   SalerDao sd = new SalerDaoImpl();
		try {
			sd.addFruitYixiang(yixiang);
			response.sendRedirect("JijieSuc.jsp");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     // TODO Auto-generated method stub
	
	}
	protected void addTuijian(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
        response.setContentType("text/utf-8;charset=utf-8");
        String TJfruit = request.getParameter("TJfruit");
        String yuefen = request.getParameter("yuefen");
        String tjly = request.getParameter("tjly");
        Tuijian tuijian = new Tuijian(TJfruit,yuefen,tjly);
        SalerDao sd = new SalerDaoImpl();
      		try {
      			sd.addTJfruit(tuijian);
      			response.sendRedirect("Tuijiansuc.jsp");
      		} catch (SQLException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
           // TODO Auto-generated method stub
	}
		protected void delTuijian(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String tuijian = request.getParameter("tuijian");
			SalerDao sdao = new SalerDaoImpl();
			try {
				sdao.delTJfruit(tuijian);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			response.sendRedirect("Tuijian.jsp");
		}
		protected void findby(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

			request.setCharacterEncoding("UTF-8");

			String name=request.getParameter("name");
			
			String like_name = "%" + name +"%";
			List<Tuijian> tuijianlist = new SalerDaoImpl().findby(like_name);
			request.setAttribute("tuijians", tuijianlist);
			request.getRequestDispatcher("Tuijian2.jsp").forward(request, response);
			}
	
		
}
